<?php
	function getSiteComponents($site_id, $set_id=0)
	{
		$i=0;
		if($set_id > 0)
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'components WHERE site_id='.$site_id.' AND set_id='.$set_id.' ORDER BY com_id ASC;'); }
		else
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'components WHERE site_id='.$site_id.' ORDER BY com_id ASC;'); }
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['set_id'] = $row['set_id'];
			$item[$i]['site_id'] = $row['site_id'];
			$item[$i]['com_id'] = $row['com_id'];
			$item[$i]['name_l'] = $row['name_l'];
			$item[$i]['name_r'] = $row['name_r'];
			$item[$i]['icon'] = $row['icon'];
			$item[$i]['path'] = $row['path'];
			$item[$i]['hfb_set'] = $row['hfb_set'];
			$item[$i]['theme_id'] = $row['theme_id'];
			$item[$i]['active'] = $row['active'];
			$i++;
		}
		return $item;
	}
	
	function getComponentAdmin($id)
	{
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."components WHERE id=".$id.";");
		while($row = mysql_fetch_array($result))
		{
			$item['id'] = $row['id'];
			$item['set_id'] = $row['set_id'];
			$item['site_id'] = $row['site_id'];
			$item['com_id'] = $row['com_id'];
			$item['name_l'] = $row['name_l'];
			$item['name_r'] = $row['name_r'];
			$item['icon'] = $row['icon'];
			$item['path'] = $row['path'];
			$item['hfb_set'] = $row['hfb_set'];
			$item['theme_id'] = $row['theme_id'];
			$item['active'] = $row['active'];
		}
		if ($item['hfb_set'] > 0)
		{
			$result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets WHERE id=".$item['hfb_set'].";");
			while($row = mysql_fetch_array($result))
			{
					$item['com_border'] = $row['border'];
					$item['com_header'] = $row['header'];
					$item['com_footer'] = $row['footer'];
					$item['com_button'] = $row['button'];
					$item['com_hbutton'] = $row['h_button'];
					$item['com_fbutton'] = $row['f_button'];
			}
		}
		return $item;
	}
	function newComponent($name_l, $name_r)
	{
		$name_l = stripNonAlphaNum($name_l); 
		$name_r = stripNonAlphaNum($name_r);
		if($name_l != '')
		{
			$insert  = "INSERT INTO ".DB_PERFIX."components (`id`, `name_l`, `name_r`, `path`, `active`)"; 
			$insert .= "VALUES (NULL, '".$name_l."', '".$name_r."', 'components/', 0);";
			$result = mysql_query($insert);
			return $result;
		}
	}
	
	function updateComponent($id, $set_id, $com_id, $name_l, $name_r, $hfb_set=0, $theme_id=0, $path='')
	{
		$name_l 	= stripNonAlphaNum($name_l); 
		$name_r 	= stripNonAlphaNum($name_r);
		$path 		= trim($path);
		
		if(!($hfb_set > 0))
		{ $hfb_set = 0; }
		if(!($theme_id > 0))
		{ $theme_id = 0; }
		
		if($name_l != '')
		{
			$update  = ' UPDATE '.DB_PERFIX.'components SET ';
			$update .= ' `set_id` = '.$set_id.', ';
			$update .= ' `com_id` = '.$com_id.', ';
			$update .= ' `name_l` = "'.$name_l.'", ';
			$update .= ' `name_r` = "'.$name_r.'", ';
			$update .= ' `hfb_set` = '.$hfb_set.', ';
			if($path != '')
			{ $update .= ' `path` = "'.$path.'", '; }
			$update .= ' `theme_id` = '.$theme_id.' ';
			$update .= ' WHERE id='.$id.' LIMIT 1; ';
			$result = mysql_query($update);
		}
	}
	
	function deleteComponent($id)
	{
		if($id > 0)
		{
			$result = mysql_query('DELETE FROM '.DB_PERFIX.'components WHERE id='.$id.' LIMIT 1;');
			return $result;
		}
	}
	
	function addComponentToSite($site, $component)
	{
		if(($component > 0) && ($site > 0))
		{
			$com = getComponentAdmin($component);
			if($com['id'] == $component)
			{
				$insert  = "INSERT INTO ".DB_PERFIX."components (`id`, `set_id`, `site_id`, `com_id`, `name_l`, `name_r`, `path`, `active`)"; 
				$insert .= "VALUES (NULL, ".$com['set_id'].", ".$site.", ".$com['com_id'].", '".$com['name_l']."', '".$com['name_r']."', '".$com['path']."', 1);";
				$result = mysql_query($insert);
				return $result;
			}
		}
	}
?>